<?php

namespace App\Models;

use Spatie\EloquentSortable\Sortable;
use Illuminate\Database\Eloquent\Model;
use Spatie\EloquentSortable\SortableTrait;
use Encore\Admin\Traits\DefaultDatetimeFormat;

class Article extends Model implements Sortable
{
    use DefaultDatetimeFormat, SortableTrait;
    protected $table = 'articles';

    protected $primaryKey = 'id';

    public $incrementing = false;
    
    public $sortable = [
        'order_column_name' => 'order',
        'sort_when_creating' => true,
        // 'ignore_timestamps' => false,
    ];
    

    public function category()
    {
        return $this->belongsTo(ArticleCategory::class, 'category_id', 'id');
    }

    public function buildSortQuery()
    {
        // 仅用同一个 app_key 的记录进行排序比较
        return static::where('app_key', $this->app_key); 
    }
}